<?php
$page = 'add_app';
require_once 'header.php';
include("Includes/FusionCharts_Gen.php");
include 'connect/connect.php';?>
<strong><a href="add_app.php">&LT;&LT;  Back to applications</a>
<a href="allUsage.php?chartType=bar" class="btn">Bar Chart</a>
<a href="allUsage.php?chartType=line" class="btn">Line Chart</a>
<a href="allUsage.php?chartType=2d" class="btn">2D Column Chart</a>
<a href="allUsage.php?chartType=3d" class="btn">3D Column  Chart</a>
</strong>

<?php
extract($_POST);
$added_by = $_SESSION['username'];
$j = 0;
$productsQ = "SELECT app_name as app_name, id as appId from apps where added_by='$added_by'";
$productsR = mysql_query($productsQ) or die("Error executing '$productsQ':" . mysql_error());
if (mysql_num_rows($productsR) > 0) {
    while ($rowP = mysql_fetch_assoc($productsR)) {
        extract($rowP);
        $arrData[$j][0] = $app_name;
        $arrData[$j][1] = "";

        $queryW = "SELECT year(date_accessed),week(date_accessed) as week
                    FROM app_usage GROUP BY year(date_accessed),week(date_accessed)order by year(date_accessed)desc,week(date_accessed)asc";
        $resultW = mysql_query($queryW) or die("Error executing '$queryW':" . mysql_error());
        $d = 2;
        if (mysql_num_rows($resultW) > 0) {
            $i = 0;
            while ($rowW = mysql_fetch_assoc($resultW)) {
                extract($rowW);
                $arrCatNames[$i] = "Week " . $i;

                $query = "SELECT count(*)as time_accessed FROM app_usage where app_id='$appId' and week(date_accessed)= $week";
                $result = mysql_query($query) or die("Error executing '$query':" . mysql_error());
                if (mysql_num_rows($result) > 0) {

                    $row = mysql_fetch_assoc($result);
                    extract($row);
                    $arrData[$j][$d] = $time_accessed;
                    $d++;
                }
                $i++;
            }
        }
        $j++;
    }
}


extract($_POST);
if (isset($_GET['chartType'])) {
    $chartType = $_GET['chartType'];

    if ($chartType == 'bar') {
        $FC = new FusionCharts("MSBar2D", "800", "500");
    } else if ($chartType == 'line') {
        $FC = new FusionCharts("MSLine", "800", "500");
    } else if ($chartType == '2d') {
        $FC = new FusionCharts("MSColumn2D", "800", "500");
    } else if ($chartType == '3d') {
        $FC = new FusionCharts("MSColumn3D", "800", "500");
    }
} else {
    $FC = new FusionCharts("MSColumn3D", "800", "500");
}# Set Relative Path of swf file. 
$FC->setSwfPath("Flash/");

# Store chart attributes in a variable
$strParam = "caption=Application Usage Comparison;xAxisName=Week;yAxisName=Times Used;decimalPrecision=0";

# Set chart attributes
$FC->setChartParams($strParam);

# Pass the 2 arrays storing data and category names to 
# FusionCharts PHP Class function addChartDataFromArray
$FC->addChartDataFromArray($arrData, $arrCatNames);

# Render the Chart 
$FC->renderChart();
require_once 'footer.php';
?>
  